import java.awt.event.HierarchyListener;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class BinaryTree {
    static class TreeNode {
        public TreeNode left;
        public TreeNode right;
        public char val;

        public TreeNode(char val) {
            this.val = val;
        }
    }
    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;

        return A;
    }

    //前序遍历
    public void preOrder(TreeNode root) {
        if(root == null) return;
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    //中序遍历
    public void inOrder(TreeNode root) {
        if(root == null) return;
        inOrder(root.left);
        System.out.print(root.val +" ");
        inOrder(root.right);
    }

    //后序遍历
    public void postOrder(TreeNode root) {
        if(root == null) return;
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val +" ");
    }

    public int node;
    //获取整个树的结点，遍历思路
    public void getNode(TreeNode root) {
        if(root == null) return;
        node++;
        getNode(root.left);
        getNode(root.right);
    }
//    //获取整棵树的结点,遍历思路
//    public int count;
//    public void getNode1(TreeNode root) {
//        if(root == null) {
//            return ;
//        }
//        count++;
//        getNode1(root.left);
//        getNode1(root.right);
//    }

    //获取整个树的结点，子问题思路
    public int getNode2(TreeNode root) {
        if(root == null) return 0;
        return getNode2(root.left) + getNode2(root.right) + 1;
    }

//    //获取整棵树的结点，子问题思路：每棵树的结点树等于左子树的节点数加上右子树的节点数再加上一
//    public int getNode3(TreeNode root) {
//        if(root == null) return 0;
//        return getNode3(root.left) + getNode3(root.right) + 1;
//    }


    public int leapNode;
    //获取叶子结点的个数，遍历思路
    public void getLeapNode(TreeNode root) {
        if(root == null) return;
        if(root.left == null && root.right == null) {
            leapNode++;
        }
        getLeapNode(root.left);
        getLeapNode(root.right);
    }
//  //获取叶子结点个数，遍历思路
//  public int leapCount;
//    public void getLeapCount(TreeNode root) {
//        if(root == null) {
//            return ;
//        }
//        if(root.left == null && root.right == null) {
//            leapCount++;
//        }
//        getLeapCount(root.left);
//        getLeapCount(root.right);
//    }



    //获取叶子结点的个数，子问题思路
    public int getLeapNode2(TreeNode root) {
        if(root == null) return 0;
        if(root.left == null && root.right == null) {
            return 1;
        }
        return getLeapNode2(root.left)+getLeapNode2(root.right);
    }


    //获取叶子结点的个数，子问题思路
    public int getLeapNod2(TreeNode root) {
        if(root == null) return 0;
        if(root.left == null && root.right == null) {
            return 1;
        }
        return getLeapNod2(root.left) + getLeapNod2(root.right);
    }













    /**
     * 第K层节点的个数
     * @param root
     * @param k
     * @return
     */
    public int getKLevelNodeCount(TreeNode root,int k) {
        if(root == null) {
            return 0;
        }
        if(k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left,k-1) +
                getKLevelNodeCount(root.right,k-1);
    }













    /**
     * 二叉树的高度
     * 时间复杂度：O(N)
     * @param root
     * @return
     */
    public int getHeight(TreeNode root) {
        if(root == null) return 0;
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);

        return Math.max(leftHeight,rightHeight) + 1;

        //return leftHeight > rightHeight ? leftHeight+1 : rightHeight+1;
        //return getHeight(root.left) > getHeight(root.right) ?
        //getHeight(root.left)+1 : getHeight(root.right)+1;
    }


















    public TreeNode find(TreeNode root,char key) {
        if(root == null) {
            return null;
        }
        if(root.val == key) {
            return root;
        }
        TreeNode leftResult = find(root.left,key);
        if(leftResult != null) {
            return leftResult;
        }

        TreeNode rightResult = find(root.right,key);
        if(rightResult != null) {
            return rightResult;
        }

        return null;
    }


    // p -> m    q -> n  时间复杂度：O(min(m,n))
    ////检验这两棵树是否相同。
    public boolean isSameTree(TreeNode p, TreeNode q) {
        //1. 一个为空 一个不为空 【结构上】
        if( (p == null && q != null) || (p != null && q == null)) {
            return false;
        }
        //2. 此时 都不为空  或者 都为空 才能走到这里
        if(p == null && q == null ) {
            return true;
        }

        if(p.val != q.val) {
            return false;
        }
        //3. 此时代码走到这里  代表：p != null && q != null  && p.val == q.val
        return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
    }
    //判断一棵树是否为另一棵树的子树
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if(root == null) {
            return false;
        }
        if(isSameTree(root,subRoot)) {
            return true;
        }
        return isSubtree(root.left,subRoot) || isSubtree(root.right,subRoot);
    }
    //翻转二叉树
    public TreeNode invertTree(TreeNode root) {
        if(root == null) return null;
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }


    //判断二叉树是否为平衡二叉树
    //先判断整体是否平衡，也就是节点的整个左子树和整个右子树是否平衡；再判断左子树内部是否平衡，右子树内部是否平衡
    //时间复杂度为O（n²）
    public boolean isBalanced(TreeNode root) {
        if(root == null) return true;
        int leftHeight = getDepth(root.left);
        int rightHeight = getDepth(root.right);
        return Math.abs(leftHeight - rightHeight) <= 1 && isBalanced(root.left) && isBalanced(root.right);
    }
    public int getDepth(TreeNode root) {
        if(root == null) return 0;
        int leftHeight = getDepth(root.left);
        int rightHeight = getDepth(root.right);
        return Math.max(leftHeight,rightHeight) + 1;
    }


    //时间复杂度为O(n),判断该二叉树是否为平衡二叉树
    public boolean isBalanced2(TreeNode root) {
        if(root == null) return true;
        return getDepth2(root) >= 0;
    }
    public int getDepth2(TreeNode root) {
        if(root == null) return 0;
        int leftHeight = getDepth2(root.left);
        if(leftHeight < 0) return -1;
        int rightHeight = getDepth2(root.right);
        if(leftHeight >= 0 && rightHeight >= 0 && Math.abs(leftHeight - rightHeight) <= 1) {
            return Math.max(leftHeight,rightHeight) + 1;
        } else {
            return -1;
        }
    }


    //判断二叉树是否是对称的二叉树
    public boolean isSymmetric(TreeNode root) {
        if(root == null) return true;
        return isSymmetric2(root.left,root.right);
    }
    public boolean isSymmetric2(TreeNode leftTree,TreeNode rightTree) {
        if(leftTree == null && rightTree != null || leftTree != null && rightTree == null) {
            return false;
        }
        if(leftTree == null && rightTree == null) {
            return true;
        }
        if(leftTree.val != rightTree.val) {
            return false;
        }
        return isSymmetric2(leftTree.left,rightTree.right) && isSymmetric2(leftTree.right,rightTree.left);
    }


    //层序遍历
    public void levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        if(root == null) return;
        queue.offer(root);
        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.print(cur.val +" ");
            if(cur.left != null) {
                queue.offer(cur.left);
            }
            if(cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }


    //层序遍历，返回值以二维数组形式返回
    public List<List<Character>> levelOrder2(TreeNode root) {
        List<List<Character>> ret = new ArrayList<>();
        if(root == null) return ret;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            int size = queue.size();
            List<Character> list = new ArrayList<>();
            while(size != 0) {
                TreeNode cur = queue.poll();
                list.add(cur.val);
                size--;
                if(cur.left != null) {
                    queue.offer(cur.left);
                }
                if(cur.right != null) {
                    queue.offer(cur.right);
                }
            }
            ret.add(list);
        }
        return ret;
    }


    //判断一棵树是否完全二叉树
    public boolean isCompleteTree(TreeNode root) {
        if(root ==null) return true;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            TreeNode cur = queue.peek();
            if(cur != null) {
                queue.poll();
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else {
                break;
            }
        }
        while (!queue.isEmpty()) {
            if(queue.peek() != null) {
                return false;
            }else {
                queue.poll();
            }
        }
        return true;
    }
    }
